//广告显示
$(window).scroll(function() {
	if($(window).scrollTop() >= $(window).height()) {
		$(".sideBar").css("display", "block");
	} else {
		$(".sideBar").css("display", "none");
	}
});

//轮播图
var setDot = function(currIndex) {
	$(".slideNum>i")
		.removeClass("cur")
		.eq(currIndex)
		.addClass("cur");
}
$(".slideNum>i").click(function() {
	var $index = $(this).index();
	$(".cell").removeClass("cur").eq($index).addClass("cur").fadeIn(600);
	setDot($index);
});
//自动轮播

    function topslide(parm) {
        this.target = parm.target;
        this.cell = $(this.target + ' .cell');
        this.alli = $(this.target + ' .slideNum i');
        this.delay = parm.delay || 300;
        this.slideDelay = parm.slideDelay || 3000;
        this.slideTrans = parm.slideTrans || 3000;
        this.controller = parm.controller || false;
        this.controllerFix = parm.controllerFix || false;
        this.effect = parm.effect || 'blink';
        this.timeCycle = null;
        this.timeMsi = null;
    }

    topslide.prototype.init = function() {
        // console.log(this.slideDelay)
        //console.log(this)
        //length = this.cell.length;
        var topslide = this;
        if (this.controller) {
            $('<div class="arNav"><div class="w1004"></div></div>').appendTo(this.target);
            $('<a href="#" class="leftAr" ></a><a href="#" class="rightAr" ></a>').appendTo($(this.target + ' .arNav .w1004'));
            $(this.target).hover(function() {

                $(topslide.target + ' .leftAr').fadeIn();
                $(topslide.target + ' .rightAr').fadeIn();
            }, function() {
                $(topslide.target + ' .leftAr').fadeOut();
                $(topslide.target + ' .rightAr').fadeOut();
            });
            $(this.target + ' .leftAr').click(function(e) {
                e.preventDefault();
                //if (topslide.curIndex()!=0) {
                //console.log(topslide.curIndex())
                clearInterval(topslide.timeCycle);
                topslide.showCell(topslide.curIndex() < 1 ? topslide.curIndex() + $('.slideNum i').length - 1 : topslide.curIndex() - 1);
                topslide.cycleSlide(); //恢复轮播
                // };
            })
            $(this.target + ' .rightAr').click(function(e) {
                e.preventDefault();
                //if (topslide.curIndex() < (topslide.alli.length-1) ) {
                clearInterval(topslide.timeCycle);
                topslide.showCell(topslide.curIndex() > $('.slideNum i').length - 2 ? 0 : topslide.curIndex() + 1);
                topslide.cycleSlide(); //恢复轮播
                // };
            })
            $(window).resize(function() {
                topslide.showcontroll();
            })
        };

        this.alli.hover(function() {
            clearInterval(topslide.timeCycle); //停止轮播   
            var _this = $(this);
            var index = _this.prevAll('i').length;
            if (!_this.hasClass('cur')) {
                topslide.timeMsi = setTimeout(function() {
                    topslide.showCell(index);
                }, topslide.delay)
            };
        }, function() {
            topslide.cycleSlide(); //恢复轮播
            clearTimeout(topslide.timeMsi);
        });

        this.cell.hover(function() {
            topslide.cell.stop(true, true);
            clearInterval(topslide.timeCycle); //停止轮播 
            var _this = $(this);
            var index = _this.prevAll('.cell').length;
            if (!_this.hasClass('cur')) {
                topslide.timeMsi = setTimeout(function() {
                    topslide.showCell(index);
                }, topslide.delay)
            };
        }, function() {
            topslide.cycleSlide(); //恢复轮播
            clearTimeout(topslide.timeMsi);
        })
        //初始化翻页控制按钮
        topslide.showcontroll();
        //轮播启动
        topslide.cycleSlide();
    }
    topslide.prototype.showCell = function(index) {
        var topslide = this;
        var target = topslide.alli.eq(index);
        var curCell = topslide.cell.eq(index);
        if (topslide.cell.filter('.easing').length > 0) {
            var preCell = topslide.cell.filter('.easing');
        } else {
            var preCell = topslide.cell.filter('.cur');
        };
        //console.log(preCell)
        curCell.find('img').each(function() {
            if ($(this).attr('original')) {
                $(this).attr('src', $(this).attr('original'));
                $(this).removeAttr('original');
            };
        })
        if (curCell.find('.prom').length > 0) {
            curCell.find('.prom').each(function() {
                if ($(this).attr('original')) {
                    $(this).css({
                        'background-image': 'url(' + $(this).attr('original') + ')'
                    });
                    $(this).removeAttr('original');
                };
            })
        };
        topslide.cell.removeClass('cur').stop().clearQueue().removeClass('easing').removeAttr('style');
        topslide.alli.removeClass('cur');
        topslide.alli.each(function() {
            if ($(this).hasClass('new')) {
                $(this).removeClass('newcur')
            };
        })
        //
        target.addClass('cur');
        if (target.hasClass('new')) {
            target.addClass('newcur');
        };
        switch (topslide.effect) {
            case 'blink':
                topslide.cell.removeClass('cur').removeAttr('style');
                preCell.hide();
                curCell.show();
                break;
            case 'fade':
                preCell.css({
                    'display': 'block'
                }).animate({
                    'opacity': 0
                }, topslide.slideTrans, function() {
                    topslide.cell.removeClass('easing').removeAttr('style');
                });
                curCell.addClass('easing');
                //console.log($('.easing'))
                curCell.css({
                    'display': 'block',
                    'opacity': 0
                }).animate({
                    'opacity': 1
                }, topslide.slideTrans, function() {
                    curCell.addClass('cur').removeClass('easing').removeAttr('style')
                });
                break;
            case 'slide':
                //
                var totalDiv = topslide.cell.parent();
                var width = topslide.cell.width();
                var totalWidth = topslide.cell.length() * width;
                totalDiv.css({
                    width: totalWidth
                });
                totalDiv.animate({
                    left: width * index
                })
                break;
            default:
                preCell.hide();
                curCell.show();
        }
        return this;
    }
    topslide.prototype.curIndex = function() {
        return $(this.target + ' .slideNum .cur').prevAll('i').length;
    }
    topslide.prototype.cycleSlide = function() {
        // console.log(this.target)
        var topslide = this;
        // console.log(topslide.target);
        setCycle = true;
        var flag;
        topslide.timeCycle = setInterval(function() {
            //console.log(parm)
            if (topslide.curIndex() < (topslide.alli.length - 1)) {
                flag = topslide.curIndex() + 1;
            } else {
                flag = 0
            };
            topslide.showCell(flag);
        }, topslide.slideDelay)
        return this;
    }
    topslide.prototype.showcontroll = function() {
        if (!this.controllerFix) {
            if ($(window).width() > 1680) {
                $(this.target + ' .leftAr').show();
                $(this.target + ' .rightAr').show();
            } else {
                $(this.target + ' .leftAr').hide();
                $(this.target + ' .rightAr').hide();
            };
        };
        return this;
    }
    var t = new topslide({
        target: '#mainSlide',
        slideDelay: 4000,
        slideTrans: 1000,
        controller: false,
        controllerFix: true,
        effect: 'fade'
    })
    t.init();
    $.get('/instant/item/show_new_consumer_banner',{},function(res){
        if(res.banner_url){
            $('.js_newPer_bar').append('<a href="http://www.mia.com/special/module/index/9121/pc" target="_blank" title=""><img src="http://img.miyabaobei.com/d1/p4/2016/09/02/51/85/5185a8a6a5aa61912f5b28c54ddcf796014668959.jpg" alt=""></a>').show();
        }
    },'json');
